﻿@model Beginner.Blog.Core.PagedList<Beginner.Blog.Models.Article>
@{
    ViewBag.Title = "文章列表";
    Layout = "~/Views/Shared/_ManageLayoutPage.cshtml";


    var categories = ViewBag.Categories as IList<Beginner.Blog.Models.Category>;
}

<blockquote class="layui-elem-quote">
    <a href="@Url.Action("Create","Article")" class="layui-btn"><i class="layui-icon">&#xe61f;</i> 创建文章</a>
</blockquote>

<blockquote class="layui-elem-quote">
    @using (Html.BeginForm("List", "Article", FormMethod.Get, new { @class = "layui-form" }))
    {
        <div class="layui-form-item blog-manage-form-item-search">
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" value="@ViewBag.ArticleTitle" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">分类</label>
                <div class="layui-input-block">
                    <select name="category" lay-filter="category">
                        <option value="0">全部分类</option>
                        @if (categories != null && categories.Count > 0)
                        {
                            foreach (var item in categories)
                            {
                                var selected = item.Id == ViewBag.CagegotyId ? "selected" : "";
                                <option value="@item.Id" @selected>@item.CategoryName</option>
                            }
                        }
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    @{
                        string all = "";
                        string normal = "";
                        string draft = "";
                        var state = ViewBag.State;
                        if (state == null)
                        {
                            all = "selected";
                        }
                        if (state == 1)
                        {
                            normal = "selected";
                        }
                        if (state == 2)
                        {
                            draft = "selected";
                        }
                    }
                    <select name="state" lay-filter="state">
                        <option value="0" @all>全部状态</option>
                        <option value="1" @normal>正常</option>
                        <option value="2" @draft>草稿</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn" lay-submit="" lay-filter="manage-search">
                    <i class="layui-icon">&#xe615;</i> 搜索
                </button>
            </div>
        </div>
                        }
</blockquote>

<div class="blog-manage-table table-responsive">
    <table class="table table-hover table-striped table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>所属分类</th>
                <th>标题</th>
                <th>作者</th>
                @{
                    var url = Request.Url.ToString();
                    if (url.IndexOf("field") == -1)
                    {
                        url = url + (url.IndexOf('?') == -1 ? "?" : "&") + "field=CreateTime&sort=" + ViewBag.CreateTimeSort;
                    }
                    else
                    {
                        url = url.Replace(url.Substring(url.LastIndexOf("sort=")), "sort=" + ViewBag.CreateTimeSort);
                    }
                }
                <th><a href="@url">创建时间</a></th>
                <th>浏览量</th>
                <th>状态</th>
                <th>排序值</th>
                <th style="text-align:center;">置顶</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @if (Model.Items != null && Model.Items.Count > 0)
            {
                foreach (var item in Model.Items)
                {
                    <tr>
                        <td>@item.Id</td>
                        <td>@item.Category.CategoryName</td>
                        <td>
                            <a href="@Url.Action("Edit", "Article", new { articleId = item.Id })">@item.Title</a>
                        </td>
                        <td>@item.Author</td>
                        <td>@item.CreateTime.ToString("yyyy-MM-dd HH:mm")</td>

                        <td>@item.Hits</td>
                        @{
                            var state = item.State == 1 ? "正常" : "草稿";
                        }
                        <td>@state</td>
                        <td>@item.Sort</td>
                        @{
                            var topText = item.IsTop ?
                                "<i class=\"layui-icon\" style=\"color:green;\">&#xe616;</i>" :
                                "<i class=\"layui-icon\" style=\"color:red;\">&#x1007;</i>";
                        }
                        <td style="text-align:center;">@Html.Raw(topText)</td>
                        <td>
                            <a href="@Url.Action("Detail", "Home", new { articleId = item.Id })" target="_blank">预览</a>
                            <a href="@Url.Action("Edit", "Article", new { articleId = item.Id })">编辑</a>
                            <a href="javascript:;" data-id="@item.Id" data-opt="del">删除</a>
                        </td>
                    </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td colspan="10" style="text-align:center;">暂无数据</td>
                                </tr>
                            }
        </tbody>
    </table>
    <div class="blog-manage-page">
        <div id="page" style="padding-left:20px;"></div>
    </div>
</div>
@section scripts{
    <script>
        layui.use(['laypage','jquery','layer'], function () {
            var laypage = layui.laypage
            ,$ = layui.jquery
            ,layer = layui.layer;
            //laypage
            laypage({
                cont: 'page'
                ,curr:@Model.CurrentPage
                , pages: @Model.TotalPages //总页数
                , groups: 5 //连续显示分页数
                ,jump:function(obj, first){
                    //得到了当前页，用于向服务端请求对应数据
                    var curr = obj.curr;
                    if(!first){
                        //layer.msg('第 '+ obj.curr +' 页');
                        location.href = '/manage/article_list_'+curr+'_'+@Model.ItemsPerPage;
                    }
                }
            });
            //绑定删除事件
            $('a[data-opt=del]').each(function(){
                $(this).on('click',function(){
                    var $this =$(this);
                    var id = $this.data('id');
                    var msg = '你真的要把  <span style="color:red;">' + id + '</span> 给删除吗？<img src="' + location.origin + '/Plugins/layui/images/face/4.gif" alt="[可怜]">'
                    var title = '萌萌的提示<img src="' + location.origin + '/Plugins/layui/images/face/7.gif" alt="[害羞]">'
                    layer.confirm(msg, {icon: 3, title:title}, function(index){
                        //do something
                        layer.msg('你好狠，居然要删除我！！！');
                        layer.close(index);
                    });
                });
            });

            var $item = $('.layui-nav > .layui-nav-item');
            $item.removeClass('layui-this').eq(1).addClass('layui-this');
        });

    </script>
}